package com.lottery.shadowbd.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.bean.Lottery;
import com.lottery.shadowbd.callback.OnMyItemClickListener;
import com.lottery.shadowbd.utils.LotteryUtils;
import com.lottery.shadowbd.utils.StringUtils;
import com.lottery.shadowbd.widget.KJLineItemView;

import java.util.List;

/**
 * Created by Administrator on 2018/3/5 0005.
 */

public class LotteryDrawAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int TYPE_TOP = 0x01;
    private final int TYPE_ITEM = 0x02;

    private Context mContext;
    private List<Lottery.IEntity> mList;

    private OnMyItemClickListener myItemClickListener;

    private StringBuilder stringBuilder;

    public LotteryDrawAdapter(Context mContext, List<Lottery.IEntity> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = null;

        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TYPE_TOP:
                AppCompatImageView img = new AppCompatImageView(parent.getContext());

                RecyclerView.LayoutParams l = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

                img.setId(R.id.bannerId);
                img.setImageResource(R.mipmap.banner);
                img.setScaleType(ImageView.ScaleType.CENTER_CROP);

                img.setLayoutParams(l);

                holder = new RecyclerView.ViewHolder(img) {};
                break;
            default:
                mView = LayoutInflater.from(mContext).inflate(R.layout.item_lot_draw, parent, false);

                holder = new ViewHolder(mView);
                break;
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)) {
            case TYPE_TOP:
                AppCompatImageView img = (AppCompatImageView) holder.itemView.findViewById(R.id.bannerId);

                if(myItemClickListener != null) {
                    img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            myItemClickListener.onItemClick(v, position);
                        }
                    });
                }
                break;
            case TYPE_ITEM:
                final ViewHolder mHolder = (ViewHolder) holder;

                Lottery.IEntity entity = mList.get(position - 1);

                mHolder.tv_lot_name.setText(entity.getLotName());

                stringBuilder = new StringBuilder("第");
                stringBuilder.append(StringUtils.getStr(entity.getIssue()));
                stringBuilder.append("期");

                mHolder.tv_lot_phase.setText(stringBuilder);

                stringBuilder = new StringBuilder("开奖日期");
                stringBuilder.append(StringUtils.getStr(entity.getDate()));

                mHolder.tv_lot_date.setText(stringBuilder);

                /*DisplayMetrics outMetrics = null;
                outMetrics = new DisplayMetrics();
                ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
                mHolder.kl_ball.setWidthHeight(outMetrics.widthPixels, outMetrics.heightPixels);*/
                mHolder.kl_ball.addViewList(LotteryUtils.getBall(entity));

                if (myItemClickListener != null) {
                    mHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            myItemClickListener.onItemClick(v, position);
                        }
                    });
                }
                break;
            default:
                break;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList != null ? (mList.size() + 1) : 1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0) {
            return TYPE_TOP;
        }

        return TYPE_ITEM;
    }

    public void setMyItemClickListener(OnMyItemClickListener myItemClickListener) {
        this.myItemClickListener = myItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        //彩票名称
        TextView tv_lot_name;
        //期
        TextView tv_lot_phase;
        //开奖日期
        TextView tv_lot_date;
        //ball
        KJLineItemView kl_ball;

        public ViewHolder(View itemView) {
            super(itemView);

            tv_lot_name = (TextView) itemView.findViewById(R.id.tv_lot_name);
            tv_lot_phase = (TextView) itemView.findViewById(R.id.tv_lot_phase);
            tv_lot_date = (TextView) itemView.findViewById(R.id.tv_lot_date);

            kl_ball = (KJLineItemView) itemView.findViewById(R.id.kl_ball);

        }
    }
}
